package com.fourhats.controller;

import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class SecurityNavigationController {

	@RequestMapping(value="/user-login", method=RequestMethod.GET)  
    public ModelAndView loginForm() {  
        return new ModelAndView("login");  
    }
	
	@RequestMapping(value="/error-login", method=RequestMethod.GET)
	public ModelAndView invalidLogin() {
		ModelAndView modelAndView = new ModelAndView("accessDenied");
		modelAndView.addObject("error", true);
		return modelAndView;
	}
	
	@RequestMapping(value="/admin/home/{userId}")  
    public @ResponseBody String admin(@PathVariable long userId) {  
        
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		if(!(auth instanceof AnonymousAuthenticationToken)){
			return "login_shown_for_ajax";	
		}
		return "login_hidden_for_ajax";
    }
	
	@RequestMapping("/loginFail")
    public String loginFailHTM() {
        return "place";
    }
	
	@RequestMapping("/pruebas")
    public ModelAndView product() {
		return new ModelAndView("pruebas");
    }
}
